package com.whut.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whut.mapper.*;
import com.whut.model.*;
import com.whut.service.ZhouqiInfoService;
import com.whut.v_model.GroupMemberDetail;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("/ZhouqiInfoService")
public class ZhouqiInfoServiceImpl implements ZhouqiInfoService {
    @Resource
    private ContracttaskacceptMapper contracttaskacceptMapper;
    @Resource
    private GroupMemberDetailMapper groupMemberDetailMapper;
    @Resource
    private ContractchecktaskMapper contractchecktaskMapper;
    @Resource
    private CheckgroupMapper checkgroupMapper;
    @Resource
    private EditableweightpsMapper editableweightpsMapper;

    @Resource
    private NewprefixsystemMapper newprefixsystemMapper;
    @Override
    public Object getTaskInfoByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Contracttaskaccept> list = contracttaskacceptMapper.getTaskInfoByPage();
        PageInfo result = new PageInfo(list);
        return result;
    }

    @Override
    public Object getTaskInfoSearchParams(String searchParams) {
        PageHelper.startPage(1, 10);
        List<Contracttaskaccept> list = contracttaskacceptMapper.getTaskInfoBySearchParams(searchParams);
        PageInfo result = new PageInfo(list);
        return result;
    }

    @Override
    public Object getGroupHead(Integer groupId) {
        List<GroupMemberDetail> result = groupMemberDetailMapper.getGroupHead(groupId);
        return result;
    }

    @Override
    public Object getGroupMemb(Integer groupId) {
        List<GroupMemberDetail> result = groupMemberDetailMapper.getGroupMemb(groupId);
        return result;
    }

    @Override
    public Object getContractCheckTaskInfo(Integer taskId, int curPage, int pageSize) {
        PageHelper.startPage(curPage, pageSize);
        List<ContractCheckTask> list = contractchecktaskMapper.getContractCheckTaskInfo(taskId);
        PageInfo result = new PageInfo(list);
        return result;
    }

    @Override
    public Object getGroupInfo() {
        List<Checkgroup> result = checkgroupMapper.getGroupInfo();
        return result;
    }

    @Override
    public Object addTaskInfo(String infotemp, String stage1temp, String stage2temp) {
        Contracttaskaccept info = JSONObject.parseObject(infotemp, Contracttaskaccept.class);
        JSONArray jsonArray1 = JSONArray.parseArray(stage1temp);
        int[] stage1 = new int[jsonArray1.size()];
        for (int i = 0; i < jsonArray1.size(); i++) {
            stage1[i] = (int) jsonArray1.get(i);
        }
        JSONArray jsonArray2 = JSONArray.parseArray(stage2temp);
        int[] stage2 = new int[jsonArray2.size()];
        for (int i = 0; i < jsonArray2.size(); i++) {
            stage2[i] = (int) jsonArray2.get(i);
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        info.setNewCreateTime(formatter.format(date));
        info.setLastEditTime(formatter.format(date));
        try {
            List<Newprefixsystem> system = new ArrayList<>();
            int taskId = 0;
            contracttaskacceptMapper.insert(info);
            taskId = info.getTaskId();
            for (int i :stage1){
                system.add(newprefixsystemMapper.selectByPrimaryKey(i));
            }
            for (int i : stage2) {
                system.add(newprefixsystemMapper.selectByPrimaryKey(i));
                List<Newprefixsystem> stage3 = newprefixsystemMapper.selectChildrenByNumber(i);
                stage3.forEach(item -> {
                    system.add(item);
                    List<Newprefixsystem> stage4 = newprefixsystemMapper.selectChildrenByNumber(item.getNumber());
                    stage4.forEach(e -> {
                        system.add(e);
                    });
                });
            }
            List<Editableweightps> newsystem = new ArrayList<Editableweightps>();
            for (int i = 0; i < system.size(); i++) {
                Editableweightps newinfo = new Editableweightps();
                newinfo.setTaskId(taskId);
                newinfo.setNumber(system.get(i).getNumber());
                newinfo.setPreNum(system.get(i).getPreNum());
                newinfo.setNumber(system.get(i).getNumber());
                newinfo.setParameterName(system.get(i).getParameterName());
                newinfo.setNodeDepth(system.get(i).getNodeDepth());
                newinfo.setSort(system.get(i).getSort());
                newinfo.setPoint(system.get(i).getPoint());
                newinfo.setWeight(system.get(i).getWeight());
                newinfo.setIsDeprecated(system.get(i).getIsDeprecated());
                newinfo.setNewCreateTime(info.getNewCreateTime());
                newinfo.setLastEditTime(info.getLastEditTime());
                newsystem.add(newinfo);
            }

          editableweightpsMapper.insertNewSystemList(newsystem);
//            newsystem.forEach(e -> {
//               editableweightpsMapper.insertSelective(e);
//            });
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public String deleteTask(int taskId) {
        try {
            contracttaskacceptMapper.deleteByPrimaryKey(taskId);
            contractchecktaskMapper.deleteAllCheckByTaskId(taskId);
            editableweightpsMapper.deleteByTaskId(taskId);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public Object insertProjectToTask(String isFinish, String creator, String createTime, int groupId, String multipleSelection, int taskId, String checkName) {
        List<ContractCheckTask> list = new ArrayList<>();
        JSONArray selection = JSONArray.parseArray(multipleSelection);
        for (int i = 0; i < selection.size(); i++) {
            JSONObject object = selection.getJSONObject(i);
            ContractCheckTask contractchecktask = new ContractCheckTask();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
            Date date = new Date(System.currentTimeMillis());
            contractchecktask.setNewCreateTime(formatter.format(date));
            contractchecktask.setLastEditTime(formatter.format(date));
            int contractId = object.getIntValue("contractId");
            contractchecktask.setContractId(contractId);
            int projectId = object.getIntValue("projectId");
            contractchecktask.setProjectId(projectId);
            String projectName = object.getString("projectName");
            contractchecktask.setProjectName(projectName);

            contractchecktask.setCreateTime(createTime);
            contractchecktask.setCreator(creator);
            contractchecktask.setIsFinish(isFinish);
            contractchecktask.setCheckGroupId(String.valueOf(groupId));
            contractchecktask.setTaskId(taskId);
            contractchecktask.setCheckName(checkName);
            list.add(contractchecktask);
        }
        System.out.println(list);
        return contractchecktaskMapper.multiInsertProjectToTask(list);
    }

    @Override
    public Object editCheckTaskById(Integer taskId, String reserve1) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        String lastEditTime = formatter.format(date);
        return contracttaskacceptMapper.editCheckTaskById(taskId, reserve1, lastEditTime);
    }

    @Override
    public Object getCheckTaskById(Integer taskId) {
        return contracttaskacceptMapper.getCheckTaskById(taskId);
    }

    @Override
    public Object getGroupInfoById(Integer groupId) {
        return checkgroupMapper.selectByPrimaryKey(groupId);
    }

    @Override
    public List<Contracttaskaccept> getAllTask() {
        return contracttaskacceptMapper.getAllTask();
    }

    @Override
    public List<Editableweightps> getInfoFromNewSystem(Integer taskId, Integer preNum, Integer nodeDepth) {
        return editableweightpsMapper.getInfoByTaskId(taskId, preNum, nodeDepth);
    }

    @Override
    public Object editNewSystemWeight(String systemList) {
        JSONArray jsonArray = JSONArray.parseArray(systemList);
        List<Editableweightps> list = JSONObject.parseArray(jsonArray.toJSONString(), Editableweightps.class);
        list.forEach(item -> {
            editableweightpsMapper.updateByPrimaryKey(item);
        });
        return "success";
    }

}
